import 'package:flutter/material.dart';
import 'package:flutter_common/widget/funAppBar.dart';
import 'package:flutter_fun_app/model/videoModel.dart';
import 'package:flutter_common/net/http.dart';
import 'package:flutter_common/widget/loadingAnim.dart';
import 'package:flutter_common/config/config.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_fun_app/utils/easyRefresh.dart';
import 'package:flutter_fun_app/page/videoListPage.dart';
import 'package:fluintl/fluintl.dart';
import 'package:flutter_fun_app/utils/strings.dart';
import 'package:flutter_fun_app/utils/easyHeader.dart';
import 'package:flutter_fun_app/utils/easyFooter.dart';

class VideoPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return VideoState();
  }
}

class VideoState extends State<VideoPage> {
  List<ItemList> videoList = List();
  GlobalKey<EasyRefreshState> easyRefreshKey =
      new GlobalKey<EasyRefreshState>();
  GlobalKey<RefreshHeaderState> headerKey = new GlobalKey<RefreshHeaderState>();
  GlobalKey<RefreshFooterState> footerKey = new GlobalKey<RefreshFooterState>();
  bool loadMore = true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((callBack) {
      easyRefreshKey.currentState.callRefresh();
    });
  }

  Future<void> getVideoData() async {
    HttpUtil.get('https://api.apiopen.top/videoCategory', success: (data) {
      VideoModel videoModel = VideoModel.fromJson(data);
      if (videoModel.code == 200) {
        setState(() {
          videoList = videoModel.result.itemList;
        });
      }
    }, then: () {});
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
        appBar: FunAppBar(
          title: IntlUtil.getString(context, Ids.title_video),
        ),
        body: new EasyRefresh(
          key: easyRefreshKey,
          refreshHeader: EasyHeader(key: headerKey),
          refreshFooter: EasyFooter(key: footerKey),
          onRefresh: () async {
            loadMore = true;
            videoList.clear();
            getVideoData();
          },
          loadMore: loadMore
              ? () async {
                  easyRefreshKey.currentState.waitState(() {
                    setState(() {
                      loadMore = false;
                    });
                  });
                }
              : null,
          child: ListView.builder(
              itemCount: videoList.length,
              itemBuilder: (BuildContext context, int index) {
                return videoBuilder(index);
              }),
        ));
  }

  Widget videoBuilder(int index) {
    ItemList item = videoList[index];
    return Card(
      color: Colors.white,
      child: ListTile(
        leading: Padding(
          padding: EdgeInsets.only(bottom: 5, top: 5),
          child: Image.network(item.data.icon),
        ),
        title: Text(
          item.data.description,
          style: TextStyle(fontSize: 14, color: Config.titleBarColor),
        ),
        subtitle: Text(item.data.title),
        onTap: () {
          Navigator.push(context,
              new MaterialPageRoute(builder: (context) => VideoListPage(item)));
        },
      ),
    );
  }
}
